package com.ldc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldc.model.SysRole;
import com.ldc.service.mapper.RoleMapper;
import com.ldc.service.service.RoleService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能说明
 *
 * @auther liudc
 * @since 2023/6/12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements RoleService {
    @Override
    public SysRole findBy(LambdaQueryWrapper<SysRole> queryWrapper) {
        return getOne(queryWrapper);
    }

    @Override
    public SysRole findByRoleId(long roleId) {
        return findBy(new LambdaQueryWrapper<SysRole>().eq(SysRole::getId, roleId));
    }

    @Override
    public SysRole findByRole(String role) {
        return findBy(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRole, role));
    }

    @Override
    public IPage<SysRole> findPageBy(IPage<SysRole> page, LambdaQueryWrapper<SysRole> queryWrapper) {
        return getBaseMapper().selectPage(page, queryWrapper);
    }

    @Override
    public IPage<SysRole> findPageByPageQuery(long page, long offset, String role) {
        return findPageBy(Page.of(page, offset, true), new LambdaQueryWrapper<SysRole>()
                .like(SysRole::getRole, role).or()
                .like(SysRole::getRoleName, role));
    }

    @Override
    public List<SysRole> findListBy(LambdaQueryWrapper<SysRole> queryWrapper) {
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<Long> findRoleIdListByRoles(List<String> roleList) {
        return findListBy(new LambdaQueryWrapper<SysRole>()
                .in(SysRole::getRole, roleList)).stream().map(SysRole::getId).collect(Collectors.toList());

    }


}
